package com.jy.framework.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

/**
 * 对象字符串处理工具
 *
 * @author JuiceYan
 * @date 2018/03/29
 */
public class FormatterUtils {
    private static final Log logger = LogFactory
            .getLog(FormatterUtils.class);

    public static <T> void handleListColumns(Collection<T> collection) {
        Function<String, String> handler = getStringStringFunction();
        collection.stream().forEach(handle -> {
            FormatterUtils.handleColumns(handle, handler);
        });
    }

    public static Function<String, String> getStringStringFunction() {
        return f -> {
            StringBuffer buffer = new StringBuffer();
            int strLen;
            if (f == null || (strLen = f.length()) == 0) {
                return "";
            }
            for (int i = 0; i < strLen; i++) {
                char ch = f.charAt(i);
                if (!Character.isWhitespace(ch)) {
                    buffer.append(ch);
                }
            }
            return buffer.toString();
        };
    }

    /**
     * @Author: JuiceYan
     * @Description: 处理字符串字段，重新赋值，主要处理去空字符
     * @param: [t, handler]
     * @return: void
     * @throws:
     * @Date: 10:20 2018/12/14
     */
    public static <T> void handleColumns(T obj, Function<String, String> handler) {
        if (null == obj) {
            return;
        }
        Class<?> aClass = obj.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        Arrays.stream(declaredFields).filter(f -> {
//                只处理String 类型
            String simpleName = f.getType().getSimpleName();
            return StringUtils.equalsIgnoreCase(simpleName, "String");
        }).forEach(f -> {
            f.setAccessible(true);
            try {
                Object value = f.get(obj);
                if (null != value) {
                    String apply = handler.apply((String) value);
                    f.set(obj, apply);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                logger.error("字段：" + f.getName() + " " + e.getMessage(), e);
            }
        });
    }

    /**
    * @Author: JuiceYan
    * @Description: 转小驼峰
    * @param: [str]
    * @return: java.lang.String
    * @throws: 
    * @Date: 22:11 2018/12/24
    */
    public static String toLowerCamel(String str) {
        String camelFlag = "_";
        if (!StringUtils.contains(str, camelFlag)) {
            return str;
        }
        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str);
    }

    /**
    * @Author: JuiceYan
    * @Description: 包含所有key
    * @param: [obj]
    * @return: java.lang.String
    * @throws: 
    * @Date: 22:16 2018/12/24
    */
    public static String toFullJsonStr(Object obj) {
        return JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue);
    }
}
